import React from 'react';


function match(path,realPath) {
    let ps=path.split('/');
    let rps=realPath.split('/');
    if(ps.length!==rps.length){
        return null
    }
    let isMatch=true;
    let params={};
    for(let i=0;i<rps.length;i++){
        let rp=rps[i];
        let p =ps[i];
        if(p.startsWith(':')){
            params[p.substr(1)]=rp;
        }else{
            if(p!==rp){
                isMatch=false;
            }
        }
    }
   if(isMatch){
       return params;
   }   
   return null;  
}

export default class MyRoute extends React.Component{
    constructor(props){
        super(props);
        console.log(props)
    }

    render(){
        const {path,component}=this.props;
        const Component=component;
        let comp=null;
        let params=match(path,window.location.pathname);
        let matchObj={
            params
        }
        if(params){
            comp=<Component match={matchObj} />
        }
        return(
            <div>
                {comp}
            </div>
        )
    }
}